// 生成空气质量数据
function generateAirQualityData() {
    const data = [];
    const now = new Date();

    for (let i = 0; i < 24; i++) {
        const time = new Date(now);
        time.setHours(now.getHours() - 23 + i);

        const value = Math.floor(Math.random() * 200) + 20;
        let level, mainPollutant;

        if (value <= 50) {
            level = '优';
            mainPollutant = 'None';
        } else if (value <= 100) {
            level = '良';
            mainPollutant = 'PM2.5';
        } else if (value <= 150) {
            level = '轻度污染';
            mainPollutant = 'PM10';
        } else if (value <= 200) {
            level = '中度污染';
            mainPollutant = 'NO2';
        } else {
            level = '重度污染';
            mainPollutant = 'O3';
        }

        data.push({
            time: time.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
            value,
            level,
            mainPollutant
        });
    }

    return data;
}

// 生成污染物排名数据
function generatePollutionRankingData() {
    const areas = ['北京市', '上海市', '广州市', '深圳市', '成都市', '重庆市', '武汉市', '西安市', '南京市', '杭州市', '天津市', '苏州市', '郑州市', '长沙市', '青岛市'];

    return areas.map(area => {
        return {
            area,
            pm25: Math.floor(Math.random() * 100) + 10,
            pm10: Math.floor(Math.random() * 150) + 20,
            no2: Math.floor(Math.random() * 80) + 10
        };
    });
}

// 生成温湿度数据
function generateTemperatureHumidityData() {
    const data = [];
    const now = new Date();

    for (let i = 0; i < 24; i++) {
        const time = new Date(now);
        time.setHours(now.getHours() - 23 + i);

        data.push({
            time: time.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
            temperature: (Math.random() * 15 + 15).toFixed(1),
            humidity: Math.floor(Math.random() * 50) + 30
        });
    }

    return data;
}

// 生成水质数据
function generateWaterQualityData() {
    const locations = ['东湖', '西湖', '南湖', '北湖', '中央水库'];

    return locations.map(location => {
        return {
            location,
            ph: (Math.random() * 3 + 6).toFixed(1),
            dissolvedOxygen: (Math.random() * 5 + 3).toFixed(1),
            turbidity: (Math.random() * 5 + 1).toFixed(1),
            conductivity: Math.floor(Math.random() * 500) + 200,
            totalPhosphorus: (Math.random() * 0.5 + 0.1).toFixed(2),
            ammoniaNitrogen: (Math.random() * 1 + 0.1).toFixed(2)
        };
    });
}

// 生成能源消耗数据
function generateEnergyConsumptionData() {
    const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];

    return months.map(month => {
        return {
            month,
            industrial: Math.floor(Math.random() * 500) + 300,
            commercial: Math.floor(Math.random() * 300) + 200,
            residential: Math.floor(Math.random() * 200) + 100,
            others: Math.floor(Math.random() * 100) + 50
        };
    });
}

// 生成监测站点数据
function generateMonitoringStationsData() {
    // 模拟中国主要城市的经纬度和AQI数据
    const stations = [
        { name: '北京站', longitude: 116.4074, latitude: 39.9042, isMainStation: true },
        { name: '上海站', longitude: 121.4737, latitude: 31.2304, isMainStation: true },
        { name: '广州站', longitude: 113.2644, latitude: 23.1291, isMainStation: true },
        { name: '深圳站', longitude: 114.0579, latitude: 22.5431, isMainStation: false },
        { name: '成都站', longitude: 104.0668, latitude: 30.5728, isMainStation: false },
        { name: '重庆站', longitude: 106.5516, latitude: 29.5630, isMainStation: false },
        { name: '武汉站', longitude: 114.3055, latitude: 30.5928, isMainStation: false },
        { name: '西安站', longitude: 108.9402, latitude: 34.3416, isMainStation: false },
        { name: '南京站', longitude: 118.7969, latitude: 32.0603, isMainStation: false },
        { name: '杭州站', longitude: 120.2052, latitude: 30.2526, isMainStation: false },
        { name: '天津站', longitude: 117.1993, latitude: 39.0851, isMainStation: false },
        { name: '苏州站', longitude: 120.5853, latitude: 31.2990, isMainStation: false },
        { name: '郑州站', longitude: 113.6254, latitude: 34.7466, isMainStation: false },
        { name: '长沙站', longitude: 112.9388, latitude: 28.2282, isMainStation: false },
        { name: '青岛站', longitude: 120.3826, latitude: 36.0671, isMainStation: false }
    ];

    return stations.map(station => {
        const aqi = Math.floor(Math.random() * 200) + 20;
        let status;

        if (aqi <= 50) status = '优';
        else if (aqi <= 100) status = '良';
        else if (aqi <= 150) status = '轻度污染';
        else if (aqi <= 200) status = '中度污染';
        else status = '重度污染';

        const now = new Date();

        return {
            ...station,
            aqi,
            status,
            updateTime: now.toLocaleString('zh-CN')
        };
    });
}

// 生成概览统计数据
function generateOverviewStatsData() {
    return [
        { label: '空气质量指数', value: Math.floor(Math.random() * 150) + 30, unit: '', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() },
        { label: '平均温度', value: (Math.random() * 15 + 15).toFixed(1), unit: '°C', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() },
        { label: '平均湿度', value: Math.floor(Math.random() * 50) + 30, unit: '%', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() },
        { label: '水质指数', value: Math.floor(Math.random() * 30) + 70, unit: '', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() },
        { label: '噪声水平', value: Math.floor(Math.random() * 30) + 40, unit: 'dB', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() },
        { label: '能源消耗', value: Math.floor(Math.random() * 1000) + 500, unit: 'kWh', trend: Math.floor(Math.random() * 20) - 10, status: getRandomStatus() }
    ];
}

// 生成随机状态
function getRandomStatus() {
    const statuses = ['good', 'normal', 'warning', 'danger'];
    return statuses[Math.floor(Math.random() * statuses.length)];
}

// 生成随机预警信息
function generateAlarmData() {
    const alarmTypes = ['warning', 'error', 'info'];
    const alarmType = alarmTypes[Math.floor(Math.random() * alarmTypes.length)];

    const alarmTitles = {
        warning: '环境指标异常预警',
        error: '环境污染严重预警',
        info: '环境指标变化提醒'
    };

    const alarmDescriptions = {
        warning: '检测到某区域环境指标超出正常范围，请注意关注。',
        error: '检测到严重污染情况，请立即采取应对措施。',
        info: '环境指标出现波动，在可接受范围内，请持续关注。'
    };

    const locations = ['北京市朝阳区', '上海市浦东新区', '广州市天河区', '深圳市南山区', '成都市武侯区'];
    const location = locations[Math.floor(Math.random() * locations.length)];

    return {
        id: Date.now().toString(),
        type: alarmType,
        title: alarmTitles[alarmType],
        description: alarmDescriptions[alarmType],
        location,
        time: new Date().toLocaleString('zh-CN')
    };
}

// 数据生成器映射
const generators = {
    airQuality: generateAirQualityData,
    pollutionRanking: generatePollutionRankingData,
    temperatureHumidity: generateTemperatureHumidityData,
    waterQuality: generateWaterQualityData,
    energyConsumption: generateEnergyConsumptionData,
    monitoringStations: generateMonitoringStationsData,
    overviewStats: generateOverviewStatsData,
    alarm: generateAlarmData
};

module.exports = {
    generators,
    generateAirQualityData,
    generatePollutionRankingData,
    generateTemperatureHumidityData,
    generateWaterQualityData,
    generateEnergyConsumptionData,
    generateMonitoringStationsData,
    generateOverviewStatsData,
    generateAlarmData
};